package cn.itcast.tools.java发送请求;

import org.springframework.http.*;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import java.net.URI;
import java.util.Arrays;

//可用
public class CreateHttpTest {

    public static void main(String[] args) {

        createHttp();
    }
    public static void createHttp() {

        RestTemplate template = new RestTemplate();
        ClientHttpRequestFactory clientFactory = new HttpComponentsClientHttpRequestFactory();
        template.setRequestFactory(clientFactory);

        String urlStr = "eas-zhangbei-b.alibaba-inc.com/api/predict/aiops_server";

        URI uri = UriComponentsBuilder.fromUriString(urlStr)
//.queryParam("sid", "42581a0dc0294e97ac8e3331875cfaaa")
                .build().encode().toUri();

        HttpHeaders requestHeaders = new HttpHeaders();
        requestHeaders.set("Authorization", "XXX");
        requestHeaders.setAccept(Arrays.asList(MediaType.APPLICATION_JSON_UTF8));
        requestHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
        HttpEntity<String> requestEntity = new HttpEntity<String>("{\"method\": \"add_group_member\" , \"time\": \"1636304160000\" , \"value\": \"0.045455882352941145\" }",requestHeaders);

        ResponseEntity<String> responseEntity = template.exchange(uri, HttpMethod.POST, requestEntity,String.class);

        System.out.println("返回信息:" + responseEntity.getBody());
    }

}